﻿package org.xhs.report.doc.model
{
    import __AS3__.vec.*;
    import baidu.lib.serialization.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.service.*;
    import baidu.wenku.common.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.utils.*;

    public class DocServiceModel extends EventDispatcher
    {
        protected var _docUrl:String = "";
        protected var _docId:String = "";
        protected var _docFpth:int = 0;
        protected var _docNpn:int = 5;
        protected var pageDataPrivider:IPageDataProvider;
        protected var screenModel:ScreenModel;
        protected var storeModel:StoreModel;
        protected var _pagesAllAvailable:int = 0;
        protected var _pagesAllTotally:int = 0;
        protected var _pagethLoading:int = 0;
        protected var _pagesLoaded:int = 0;
        protected var pagesLoaderArray:Vector.<Loader>;
        protected var pagesBitmapArray:Vector.<BitmapData>;
        protected var fromPage:Number;
        protected var toPage:Number;
        protected var binaryData:ByteArray;
        protected var _inConverting:Boolean = false;
        protected var continueConvert:Boolean = false;
        protected var pagesConvertingArray:Array;
        protected var hasConvertPages:int;
        protected var allConvertPages:int;
        protected var convertLoader:Loader;
        protected var _isStore:Boolean = false;
        protected var _storeFreePages:int = 1;
        protected var _storeCookie:String;
        protected const SWF_VERSIONS:Array;

        public function DocServiceModel(param1:IEventDispatcher = null)
        {
            this.SWF_VERSIONS = [5, 6, 7, 8, 9, 10, 11];
            super(param1);
            this.init();
            return;
        }// end function

        protected function init() : void
        {
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.storeModel = InstanceManager.createSingletonInstance(StoreModel);
            this.initParams();
            this.initDataProvider();
            return;
        }// end function

        protected function initParams() : void
        {
            var _loc_1:* = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this._docUrl = _loc_1.docUrl;
            this._docId = _loc_1.docId;
            this._docFpth = 0;
            this._docNpn = _loc_1.docNpn;
            return;
        }// end function

        protected function initDataProvider() : void
        {
            this.binaryData = new ByteArray();
            this.binaryData.endian = Endian.LITTLE_ENDIAN;
            this.pageDataPrivider = new PageDataProvider();
            this.pageDataPrivider.addEventListener(PageDataEvent.START_LOAD, this.pageDataStart);
            this.pageDataPrivider.addEventListener(PageDataEvent.LOAD_PROGRESS, this.pageDataProgress);
            this.pageDataPrivider.addEventListener(PageDataEvent.LOAD_SUCCESS, this.pageDataSuccess);
            this.pageDataPrivider.addEventListener(PageDataEvent.LOAD_FAIL, this.pageDataFail);
            return;
        }// end function

        protected function pageDataStart(event:PageDataEvent) : void
        {
            dispatchEvent(new PageServiceEvent(PageServiceEvent.START_LOADING));
            return;
        }// end function

        protected function pageDataProgress(event:PageDataEvent) : void
        {
            var _loc_5:ByteArray = null;
            this.binaryData = this.pageDataPrivider.baidu.wenku.common.service:IPageDataProvider::binaryData;
            var _loc_2:* = this.binaryData.position;
            var _loc_3:* = _loc_2;
            var _loc_4:* = this.binaryData.length;
            while (_loc_3 < _loc_4)
            {
                
                if (_loc_3 > _loc_2 && _loc_3 + 3 < _loc_4)
                {
                    if ((this.binaryData[_loc_3] == 67 || this.binaryData[_loc_3] == 70) && this.binaryData[(_loc_3 + 1)] == 87 && this.binaryData[_loc_3 + 2] == 83 && this.SWF_VERSIONS.indexOf(this.binaryData[_loc_3 + 3]) >= 0)
                    {
                        _loc_5 = new ByteArray();
                        _loc_5.endian = Endian.LITTLE_ENDIAN;
                        this.binaryData.position = _loc_2;
                        this.binaryData.readBytes(_loc_5, 0, _loc_3 - _loc_2);
                        _loc_2 = _loc_2 + (_loc_3 - _loc_2);
                        this.binaryData.position = _loc_2;
                        this.parseBinaryDate(_loc_5);
                    }
                }
                _loc_3++;
            }
            return;
        }// end function

        protected function pageDataSuccess(event:PageDataEvent) : void
        {
            this.binaryData = this.pageDataPrivider.baidu.wenku.common.service:IPageDataProvider::binaryData;
            var _loc_2:int = 0;
            var _loc_3:* = this.binaryData.length - 4;
            while (_loc_3 >= 0)
            {
                
                if ((this.binaryData[_loc_3] == 67 || this.binaryData[_loc_3] == 70) && this.binaryData[(_loc_3 + 1)] == 87 && this.binaryData[_loc_3 + 2] == 83 && this.SWF_VERSIONS.indexOf(this.binaryData[_loc_3 + 3]) >= 0)
                {
                    _loc_2 = _loc_3;
                    break;
                }
                _loc_3 = _loc_3 - 1;
            }
            var _loc_4:* = new ByteArray();
            new ByteArray().endian = Endian.LITTLE_ENDIAN;
            this.binaryData.position = _loc_2;
            this.binaryData.readBytes(_loc_4);
            this.parseBinaryDate(_loc_4);
            return;
        }// end function

        protected function parseBinaryDate(param1:ByteArray) : void
        {
            var docInfoStr:String;
            var docInfo:Object;
            var binary:* = param1;
            binary.position = 0;
            if ((binary[0] == 67 || binary[0] == 70) && binary[1] == 87 && binary[2] == 83 && this.SWF_VERSIONS.indexOf(binary[3]) >= 0)
            {
                if (!this.pagesConvertingArray)
                {
                    this.pagesConvertingArray = [];
                }
                this.pagesConvertingArray.push(this.removeActionFromSWF(binary));
                var _loc_3:String = this;
                var _loc_4:* = this._pagesLoaded + 1;
                _loc_3._pagesLoaded = _loc_4;
                if (!this._inConverting)
                {
                    this.continueConvert = false;
                    this.byteArr2DisplayObj(this.hasConvertPages);
                }
                else
                {
                    this.continueConvert = true;
                }
            }
            else
            {
                docInfoStr = binary.readMultiByte(binary.bytesAvailable, "utf-8");
                Logger.debug(docInfoStr);
                try
                {
                    docInfo = JSON.decode(docInfoStr);
                }
                catch (e:Error)
                {
                    Logger.error("parseBinaryDate, failed to parse json of doc_info: " + e);
                }
                this._pagesAllAvailable = Number(docInfo["totalPage"]);
                this._pagesAllTotally = this.pagesAllAvailable;
                if (this._isStore && this.storeModel.hasRecom)
                {
                    if (this._storeFreePages > 0)
                    {
                        this._pagesAllAvailable = int(Math.min(this._storeFreePages, this._pagesAllAvailable)) + 1;
                    }
                    else
                    {
                        (this._pagesAllAvailable + 1);
                    }
                }
                this._pagesAllTotally = Math.max(this.pagesAllAvailable, this._pagesAllTotally);
                if (this._pagesAllTotally < 0)
                {
                    this.pageDataPrivider.stopLoad();
                    dispatchEvent(new PageServiceEvent(PageServiceEvent.FILE_DELETED));
                    return;
                }
                this.toPage = Number(docInfo["toPage"]);
                this.fromPage = Number(docInfo["fromPage"]);
                this._pagethLoading = this.fromPage - 1;
                this.hasConvertPages = 0;
                this.allConvertPages = this.toPage - this.fromPage + 1;
                if (this.allConvertPages > 0)
                {
                    dispatchEvent(new PageServiceEvent(PageServiceEvent.DOCINFO_LOADED));
                }
                else
                {
                    dispatchEvent(new PageServiceEvent(PageServiceEvent.FILE_NO_DATA));
                }
            }
            return;
        }// end function

        protected function byteArr2DisplayObj(param1:uint) : void
        {
            var loaderContext:LoaderContext;
            var i:* = param1;
            if (i == 0)
            {
                dispatchEvent(new PageServiceEvent(PageServiceEvent.FIRST_PARSE_BYTE));
            }
            this._inConverting = true;
            try
            {
                loaderContext = new LoaderContext();
                this.convertLoader = new Loader();
                this.convertLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onConvertFinished);
                this.convertLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onConvertError);
                this.convertLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onConvertError);
                this.convertLoader.loadBytes(this.pagesConvertingArray[i] as ByteArray, loaderContext);
            }
            catch (e:Error)
            {
                Logger.error("byteArr2DisplayObj, Failed to byteArr2DisplayObj: " + e);
                _inConverting = false;
            }
            return;
        }// end function

        protected function onConvertFinished(event:Event) : void
        {
            this._inConverting = false;
            var _loc_3:String = this;
            var _loc_4:* = this.hasConvertPages + 1;
            _loc_3.hasConvertPages = _loc_4;
            if (!this.pagesLoaderArray)
            {
                this.pagesLoaderArray = new Vector.<Loader>(this.pagesAllAvailable);
            }
            this.pagesLoaderArray[this._pagethLoading] = this.convertLoader;
            var _loc_2:* = new PageServiceEvent(PageServiceEvent.PAGE_LOADED);
            _loc_2.pageth = this._pagethLoading;
            dispatchEvent(_loc_2);
            var _loc_3:String = this;
            var _loc_4:* = this._pagethLoading + 1;
            _loc_3._pagethLoading = _loc_4;
            if (this.hasConvertPages == this.allConvertPages)
            {
                this.hasConvertPages = 0;
                this.allConvertPages = 0;
                this.convertLoader = null;
                this.pagesConvertingArray = [];
                this.binaryData = null;
            }
            else if (this.continueConvert && this.hasConvertPages < this.pagesConvertingArray.length)
            {
                this.byteArr2DisplayObj(this.hasConvertPages);
            }
            return;
        }// end function

        protected function removeActionFromSWF(param1:ByteArray) : ByteArray
        {
            var swfInfo:Object;
            var tag:Object;
            var swfBytes:* = param1;
            try
            {
                swfInfo = SWFUtils.analyseSWF(swfBytes);
            }
            catch (e:Error)
            {
                return swfBytes;
            }
            var tags:* = swfInfo.tags;
            var i:uint;
            var len:* = tags.length;
            while (i < len)
            {
                
                tag = tags[i];
                if (tag.id == 72 || tag.id == 82 || tag.id == 76)
                {
                    tags.splice(i, 1);
                    i = (i - 1);
                    len = (len - 1);
                }
                i = (i + 1);
            }
            return SWFUtils.combineSWF(swfInfo);
        }// end function

        protected function pageDataFail(event:PageDataEvent) : void
        {
            Logger.error("pageDataFail, 加载数据错误，页码: " + this._pagethLoading);
            var _loc_2:* = new PageServiceEvent(PageServiceEvent.PAGE_FAILED);
            _loc_2.pageth = this._pagethLoading;
            dispatchEvent(_loc_2);
            var _loc_3:String = this;
            var _loc_4:* = this._pagethLoading + 1;
            _loc_3._pagethLoading = _loc_4;
            return;
        }// end function

        protected function onConvertError(event:Event = null) : void
        {
            Logger.error("onConvertError, " + event);
            this._inConverting = false;
            var _loc_3:String = this;
            var _loc_4:* = this.hasConvertPages + 1;
            _loc_3.hasConvertPages = _loc_4;
            var _loc_2:* = new PageServiceEvent(PageServiceEvent.PARSE_FAILED);
            _loc_2.pageth = this._pagethLoading;
            dispatchEvent(_loc_2);
            var _loc_3:String = this;
            var _loc_4:* = this._pagethLoading + 1;
            _loc_3._pagethLoading = _loc_4;
            if (this.hasConvertPages == this.allConvertPages)
            {
                this.hasConvertPages = 0;
                this.allConvertPages = 0;
                this.convertLoader = null;
                this.pagesConvertingArray = [];
                this.binaryData = null;
            }
            else if (this.continueConvert)
            {
                this.byteArr2DisplayObj(this.hasConvertPages);
            }
            return;
        }// end function

        protected function clear() : void
        {
            this._pagesLoaded = 0;
            this._pagesAllAvailable = 0;
            this._pagesAllTotally = 0;
            this._pagethLoading = 0;
            this.pagesLoaderArray = null;
            this.pagesBitmapArray = null;
            return;
        }// end function

        public function loadData() : void
        {
            this.pageDataPrivider.docUrl = this._docUrl;
            this.pageDataPrivider.docId = this._docId;
            this.pageDataPrivider.docFpth = this._docFpth;
            this.pageDataPrivider.docNpn = this._docNpn;
            this.pageDataPrivider.startLoad();
            return;
        }// end function

        public function stopLoad() : void
        {
            this.pageDataPrivider.stopLoad();
            return;
        }// end function

        public function isPagethLoaded(param1:int) : Boolean
        {
            if (this.pagesLoaderArray && param1 < this.pagesLoaderArray.length)
            {
                return this.pagesLoaderArray[param1] ? (true) : (false);
            }
            return false;
        }// end function

        public function getaPageLoader(param1:int) : Loader
        {
            if (this.pagesLoaderArray && param1 < this.pagesLoaderArray.length)
            {
                return this.pagesLoaderArray[param1] ? (this.pagesLoaderArray[param1]) : (null);
            }
            return null;
        }// end function

        public function getaPageBitmap(param1:int) : BitmapData
        {
            var _loc_2:Loader = null;
            var _loc_3:BitmapData = null;
            if (this.pagesLoaderArray && param1 < this.pagesLoaderArray.length)
            {
                _loc_2 = this.pagesLoaderArray[param1] as Loader;
                if (_loc_2)
                {
                    if (!this.pagesBitmapArray)
                    {
                        this.pagesBitmapArray = new Vector.<BitmapData>(this.pagesAllAvailable);
                    }
                    _loc_3 = this.pagesBitmapArray[param1];
                    if (!_loc_3)
                    {
                        _loc_3 = new BitmapData(_loc_2.width, _loc_2.height);
                        this.screenModel.stageQulity = StageQuality.BEST;
                        _loc_3.draw(_loc_2);
                        this.pagesBitmapArray[param1] = _loc_3;
                        this.screenModel.stageQulity = StageQuality.LOW;
                    }
                    return _loc_3;
                }
                return null;
            }
            return null;
        }// end function

        public function get docUrl() : String
        {
            return this._docUrl;
        }// end function

        public function set docUrl(param1:String) : void
        {
            this._docUrl = param1;
            return;
        }// end function

        public function get docId() : String
        {
            return this._docId;
        }// end function

        public function set docId(param1:String) : void
        {
            this._docId = param1;
            return;
        }// end function

        public function get docFpth() : int
        {
            return this._docFpth;
        }// end function

        public function set docFpth(param1:int) : void
        {
            this._docFpth = param1;
            return;
        }// end function

        public function get docNpn() : int
        {
            return this._docNpn;
        }// end function

        public function set docNpn(param1:int) : void
        {
            this._docNpn = param1;
            return;
        }// end function

        public function get pagesAllAvailable() : int
        {
            return this._pagesAllAvailable;
        }// end function

        public function get pagesAllTotally() : int
        {
            return this._pagesAllTotally;
        }// end function

        public function get pagesLoaded() : int
        {
            return this._pagesLoaded;
        }// end function

        public function get isLoading() : Boolean
        {
            return this.pageDataPrivider.isLoading;
        }// end function

        public function get inConverting() : Boolean
        {
            return this._inConverting;
        }// end function

        public function get isStore() : Boolean
        {
            return this._isStore;
        }// end function

        public function set isStore(param1:Boolean) : void
        {
            this._isStore = param1;
            this.pageDataPrivider.isPost = this._isStore;
            return;
        }// end function

        public function get storeCookie() : String
        {
            return this._storeCookie;
        }// end function

        public function set storeCookie(param1:String) : void
        {
            this._storeCookie = param1;
            this.pageDataPrivider.docBduss = param1;
            return;
        }// end function

        public function get storeFreePages() : int
        {
            return this._storeFreePages;
        }// end function

        public function set storeFreePages(param1:int) : void
        {
            this._storeFreePages = param1;
            return;
        }// end function

        public function get pagethLoading() : int
        {
            return this._pagethLoading;
        }// end function

    }
}
